package leetcode.code0532;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int findPairs(int[] nums, int k) {
		int len = nums.length;
		if (len < 2) {
			return 0;
		}
		Arrays.sort(nums);
		int l = 0, r = 1;
		int last = Integer.MIN_VALUE;
		int ans = 0;
		while (r < len) {
			if (nums[r] != last) {
				int abs = Math.abs(nums[r] - nums[l]);
				if (abs == k) {
					ans++;
					last = nums[r];
					l++;
					r++;
				} else if (abs > k && l + 1 < r) {
					l++;
				} else {
					r++;
				}
			} else {
				r++;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
